import { _decorator, Component, Node, UITransform, Sprite, Animation, SpriteFrame, AnimationClip, animation } from 'cc'
const { ccclass, property } = _decorator

import { EntityManager } from '../../Base/EntityManager'

// 状态
import State from '../../Base/State'

// 状态机类型
import { FSM_PARMERS_TYPE_ENUM, PLAYER_STATE_TYPE_ENUM } from '../../Enums'

// 状态机
import { StateMchine, getPlayerState, getPlayerDirection } from '../../Base/StateMchine'

// 子状态机
import { PlayerIdleStateMchine } from './PlayerIdleStateMchine'
import { PlayerTrnLeftStateMchine } from './PlayerTrnLeftStateMchine'
import { PlayerTrnRightStateMchine } from './PlayerTrnRightStateMchine'
import { PlayerBlockFrontStateMchine } from './PlayerBlockFrontStateMchine'
import { PlayerBlockBackStateMchine } from './PlayerBlockBackStateMchine'
import { PlayerBlockLeftStateMchine } from './PlayerBlockLeftStateMchine'
import { PlayerBlockRightStateMchine } from './PlayerBlockRightStateMchine'
import { PlayerBlockTurnLeftStateMchine } from './PlayerBlockTurnLeftStateMchine'
import { PlayerBlockTurnRightStateMchine } from './PlayerBlockTurnRightStateMchine'
import {PlayerDeathStateMchine} from './PlayerDeathStateMchine'
import {PlayerAttackStateMchine} from './PlayerAttackStateMchine' //攻击动画
import {AIRDeathStateMchine} from './AIRDeathStateMchine'

export type pamersValueType = number | string | boolean


export const playerWhiteList = ['block', 'turn', 'attack']

@ccclass('PlayerStateMchine')
export class PlayerStateMchine extends StateMchine {
  async init() {
    this.animationComponent = this.node.addComponent(Animation)

    this.initPramers() //初始化参数
    this.initStateMachines() //初始化状态机
    this.initAnimationEvent()

    await Promise.all(this.waitingList)
  }

  // 初始化参数
  initPramers() {
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.IDLE, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.TURN_LEFT, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.TURN_RIGHT, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.BLOCKFRONT, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.BLOCKBACK, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.BLOCKLEFT, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.BLOCKRIGHT, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.BLOCKTURNLEFT, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.BLOCKTURNRIGHT, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.DEATH, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.AIR_DEATH, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.ATTACK, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.DIRECTION, getPlayerDirection())
  }

  // 初始化状态机
  initStateMachines() {
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.IDLE, new PlayerIdleStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.TURN_LEFT, new PlayerTrnLeftStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.TURN_RIGHT, new PlayerTrnRightStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.BLOCKFRONT, new PlayerBlockFrontStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.BLOCKBACK, new PlayerBlockBackStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.BLOCKLEFT, new PlayerBlockLeftStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.BLOCKRIGHT, new PlayerBlockRightStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.BLOCKTURNLEFT, new PlayerBlockTurnLeftStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.BLOCKTURNRIGHT, new PlayerBlockTurnRightStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.DEATH, new PlayerDeathStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.AIR_DEATH, new AIRDeathStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.ATTACK, new PlayerAttackStateMchine(this))
  }

  initAnimationEvent() {
    this.animationComponent.on(Animation.EventType.FINISHED, this.onAnimationEnd, this)
  }

  // 监听动画结束
  onAnimationEnd(event: Animation.EventType, clip: AnimationClip, target: Node) {
    // // 动画结束白名单,需要回到idle状态
    // const whiteList = ['block', 'turn', 'attack']
    let res = false
    for (let i = 0; i < playerWhiteList.length; i++) {
      if (clip.name.indexOf(playerWhiteList[i]) !== -1) {
        res = true
        break
      }
    }
    if (res) {
      // this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.IDLE) as State
      // 获取基类
      this.node.getComponent(EntityManager).state = PLAYER_STATE_TYPE_ENUM.IDLE
    }
  }

  //  定义transition
  run() {
    switch (this.currentState) {
      case this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.IDLE):
      case this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.TURN_LEFT):
      case this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.TURN_RIGHT):
      case this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.BLOCKFRONT):
      case this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.BLOCKTURNLEFT):
      case this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.BLOCKTURNRIGHT):
      case this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.ATTACK):
      case this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.DEATH):
      case this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.AIR_DEATH):
        if (this.getPramers(PLAYER_STATE_TYPE_ENUM.DEATH)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.DEATH) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.AIR_DEATH)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.AIR_DEATH) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.ATTACK)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.ATTACK) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.BLOCKFRONT)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.BLOCKFRONT) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.BLOCKBACK)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.BLOCKBACK) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.BLOCKLEFT)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.BLOCKLEFT) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.BLOCKRIGHT)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.BLOCKRIGHT) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.BLOCKTURNLEFT)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.BLOCKTURNLEFT) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.BLOCKTURNRIGHT)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.BLOCKTURNRIGHT) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.TURN_LEFT)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.TURN_LEFT) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.TURN_RIGHT)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.TURN_RIGHT) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.IDLE)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.IDLE) as State
        } else {
          this.currentState = this.currentState as State
        }
        // console.log('[ currentState ] >', this.currentState)
        break
      default:
        this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.IDLE) as State
    }
  }
}
